<template>
	<view>
		<cc-addressSet :colors="colors" :addressList="addressList" @chooseClick="chooseClick"
			@editClick="editClick" @delAddress="delAddress"></cc-addressSet>

		<view class="save">
			<view class="btn" :style="'background:' + colors" @tap="addAddress('add')">添加收货地址</view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				source: 0,
				colors: '#f23a3a',
				addressList: [{
					name: '小明',
					phone: '15632678915',
					address: '佛山市南海区',
					moreAddres: '桂城街道',
					isdefult: 0,
					id: 1
				}, {
					name: '小红',
					phone: '15632678915',
					address: '广州市海珠区',
					moreAddres: '昌岗路15号',
					isdefult: 0,
					id: 2
				}],
				type:'carGots'
			}
		},
		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad(option) {
			this.source = option.source;
			if (option.data) {
				this.refreshList(option.type, option.data);
			}
			uni.setNavigationBarTitle({
				title: '收货地址'
			})
		},
		methods: {
			// 编辑地址
			editClick(item,index) {
				this.addAddress('edit', item);
			},
			// 选中地址
			chooseClick(item) {
				uni.navigateTo({
					url:`/pages/order/createOrder?type=${this.type}&data=${JSON.stringify(item)}`
				})
			},
			// 增加地址
			addAddress(type, item) {
				uni.navigateTo({
					url: `/pages/testCar/testCar?type=${type}&data=${JSON.stringify(item)}`,
				})
			},
			refreshList(type, data) {
				//添加或修改后事件，
				let item = JSON.parse(data);
				// type=='add'?this.addressList.push(JSON.parse(data)):;
				if(type == 'add'){
					this.addressList.push(item);
				}else{
					// 通过判断addressList中的id与item的id是否相等，相等则替换
					for(let i=0;i<this.addressList.length;i++){
						if(this.addressList[i].id == item.id){
							this.addressList[i] = item;
							break;
						}
					}
				}
			}
		}
	}
</script>

<style>
	.save {
		position: fixed;
		bottom: 0;
		width: 100%;
		height: 120upx;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.save view {
		display: flex;
	}

	.save .btn {
		box-shadow: 0upx 5upx 10upx rgba(0, 0, 0, 0.4);
		width: 70%;
		height: 80upx;
		border-radius: 80upx;
		background-color: #f23a3a;
		color: #fff;
		justify-content: center;
		align-items: center;
		font-size: 30upx;
		margin-bottom: 60px;
	}
</style>